#include<stdio.h>
#include<stdlib.h>
int max_heapify(int *a,int i,int size)
{

	int l=2*i-1;
	int r=2*i;
	i=i-1;
	int asize=size;
	int largest,temp;
	printf("size of a %d\n",asize);
	if(l>asize) return 0;
	if((l<asize)&&(a[l]>a[i]))
	{
		largest=l;
	}
	else
	{
		largest=i;
	}
	if((r<asize)&&(a[r]>a[largest]))
	{
		largest=r;
	}
	if(largest!=i)
	{
		temp=a[i];
		a[i]=a[largest];
		a[largest]=temp;
		max_heapify(a,largest+1,size);
	}
}

int build_heap(int *a,int size)
{
	int i;
	for(i=size/2;i>0;i--) max_heapify(a,i,size);
	return 0;
}

int heap_sort(int *a,int size)
{
	int temp,i;
	build_heap(a,size);
	for(i=0;i<size;i++) printf("%d, ",a[i]);
	for(i=size;i>1;i--)
	{
		temp=a[0];
		a[0]=a[size-1];
		a[size-1]=temp;
		size=size-1;
		max_heapify(a,1,size);
	}
}

int main()
{
	int i,j,k,n;
	int size=11;
	int b[11]={2,3,5,2,4,3,6,1,7,9,8};
	printf("size of b is :  %d\n",sizeof(b)/sizeof(int));
	heap_sort(b,size);
	for(i=0;i<11;i++) printf("%d ",b[i]);
	printf("\n");
	return 0;
}
